package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;

import com.sun.org.apache.bcel.internal.generic.DDIV;
import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.*;
import com.ymttest.utils.YMTDateUtil;

public class ExpWapper {
	SqlManager manage;

	public ExpWapper() {
		manage = new SqlManager("Exp");
	}

	/**
	 * 插入FestivalRateConfig
	 * @param userid
	 * @return
	 */
	public Integer insertFestivalRateConfig(FestivalRateConfig frc){
		Integer i=manage.insert(
				"com.ymttest.database.mapping.ExpMapper.insertFestivalRateConfig", frc);
		manage.commit();
		return i;
	}
	public Integer deleteFestivalRateConfig(String date){
		Map m=new HashMap<>();
		m.put("date", date);
		Integer i=manage.delete(
				"com.ymttest.database.mapping.ExpMapper.deleteFestivalRateConfig", m);
		manage.commit();
		return i;
	}
	public Integer deleteuserexp(int uid){
		Map m=new HashMap<>();
		m.put("uid", uid);
		Integer i=manage.delete(
				"com.ymttest.database.mapping.ExpMapper.deleteuserexp", m);
		manage.commit();
		return i;
	}
	public Integer deleteuserexpdetail(int uid,String bizcode,String bizno,String ono){
		Map m=new HashMap<>();
		m.put("uid", uid);
		m.put("bizcode", bizcode);
		m.put("bizno", bizno);
		m.put("ono", ono);
		Integer i=manage.delete(
				"com.ymttest.database.mapping.ExpMapper.deleteuserexpdetail", m);
		manage.commit();
		return i;
	}
	public List<PrivilegeConfig> selectprivilegeconfig(){
		List<PrivilegeConfig> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectprivilegeconfig", null);
		manage.commit();
		return list;
	}
	public Map selectOrderPrice1(){
		List list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectOrderPrice1", null);
		manage.commit();
		if(list!=null&&!list.isEmpty()){
			return (Map)list.get(0);
		}else{
			return null;
		}
		
	}
	public List<UserExp> selectuserexp(Integer uid) throws InterruptedException{
		Map m=new HashMap<>();
		m.put("uid", uid);
		List<UserExp> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectuserexp", m);
		manage.commit();
		if(list.size()==0){
			Thread.sleep(10000);
			 list=manage.getSelectResults(
						"com.ymttest.database.mapping.ExpMapper.selectuserexp", m);
		}
		manage.commit();
		return list;
	}
	public List<LevelConfig> selectlevelconfig(Integer lv){
		Map m=new HashMap<>();
		m.put("lv", lv);
		List<LevelConfig> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectlevelconfig", m);
		manage.commit();
		return list;
	}
	/**
	 * 查询小于此经验用户数
	 * @param exp
	 * @return
	 */
	public Long selectLtExp(Integer exp){
		Map m=new HashMap<>();
		m.put("exp", exp);
		List<Map> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectLtExp", m);
		manage.commit();
		long num=(long) list.get(0).get("num");
		return num;
	}
	public List<UserExpDetail> selectuserexpdetail(Integer uid,String bizcode,String bizno,String ono) throws InterruptedException{
		Map m=new HashMap<>();
		m.put("uid", uid);
		m.put("bizcode", bizcode);
		m.put("bizno", bizno);
		m.put("ono", ono);
		List<UserExpDetail> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectuserexpdetail", m);
		manage.commit();
		if(list==null||list.size()==0){
			Thread.sleep(5000);
			list=manage.getSelectResults(
					"com.ymttest.database.mapping.ExpMapper.selectuserexpdetail", m);
			manage.commit();
		}
		return list;
	}
	/**
	 * 根据用户和年份查询金币表数据
	 * @param uid
	 * @param year
	 * @return
	 */
	public List<UserCoin> selectUserCoin(Integer uid,String year){
		Map m=new HashMap<>();
		m.put("userid", uid);
		m.put("year", year);
		List<UserCoin> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectUserCoin", m);
		manage.commit();
		return list;
	}
	/**
	 * 根据用户和年份删除金币表数据
	 * @param uid
	 * @param year
	 * @return
	 */
	public int deleteUserCoin(Integer uid,String year){
		Map m=new HashMap<>();
		m.put("userid", uid);
		m.put("year", year);
		int i=manage.delete(
				"com.ymttest.database.mapping.ExpMapper.deleteUserCoin", m);
		manage.commit();
		return i;
	}
	/**
	 * 查询金币流水记录
	 * @param uid
	 * @param BizCode
	 * @param BizNo
	 * @param OriginalNo
	 * @return
	 */
	public List<UserCoinRecord> selectUserCoinRecord(Integer uid,String BizCode,String BizNo,String OriginalNo){
		Map m=new HashMap<>();
		m.put("userid", uid);
		m.put("BizCode", BizCode);
		m.put("BizNo", BizNo);
		m.put("OriginalNo", OriginalNo);
		List<UserCoinRecord> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectUserCoinRecord", m);
		manage.commit();
		return list;
	}
	/**
	 * 根据uid.operateType 查询流水
	 * @param uid
	 * @param operateType 0全部 1收入 2支出
	 * @return
	 */
	public List<UserCoinRecord> selectUserCoinRecord(Integer uid,Integer operateType){
		Map m=new HashMap<>();
		m.put("userid", uid);
		m.put("operateType", operateType);
		List<UserCoinRecord> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectUserCoinRecord", m);
		manage.commit();
		return list;
	}
	/**
	 * 获取金币配置
	 * @return
	 */
	public List<CoinBizcodeConfig> selectCoinBizcodeConfig(String bizcode){
		List<CoinBizcodeConfig> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectCoinBizcodeConfig", bizcode);
		manage.commit();
		return list;
	}
	/**
	 * 查询订单商品数缓存数据
	 * @param Orderid
	 * @return
	 */
	public List<Map> selectOrderCatalogNum(String Orderid){
		List<Map> list=manage.getSelectResults(
				"com.ymttest.database.mapping.ExpMapper.selectOrderCatalogNum", Orderid);
		manage.commit();
		return list;
	}
	/**
	 * 删除金币流水记录
	 * @param uid
	 * @param BizCode
	 * @param BizNo
	 * @param OriginalNo
	 * @return
	 */
	public int deleteUserCoinRecord(Integer uid,String BizCode,String BizNo,String OriginalNo){
		Map m=new HashMap<>();
		m.put("userid", uid);
		m.put("BizCode", BizCode);
		m.put("BizNo", BizNo);
		m.put("OriginalNo", OriginalNo);
		int i=manage.delete(
				"com.ymttest.database.mapping.ExpMapper.deleteUserCoinRecord", m);
		manage.commit();
		return i;
	}
	/**
	 * 插入coin记录用于测试去年前年记录用
	 * @param uc
	 * @return
	 */
	public Integer insertUserCoin(UserCoin uc){
		if(uc.getCreateTime()==null){
			uc.setCreateTime(new Date());
		}
		if(uc.getLastUpdateTime()==null){
			uc.setLastUpdateTime(new Date());
		}
		Integer i=manage.insert(
				"com.ymttest.database.mapping.ExpMapper.insertUserCoin", uc);
		manage.commit();
		return i;
	}
	/**
	 * 插入订单记录
	 * @param OrderId
	 * @param CatalogNum
	 * @return
	 */
	public Integer insertOrderCatalogNum(Long OrderId,Integer CatalogNum){
		Map m=new HashMap<>();
		m.put("OrderId", OrderId);
		m.put("CatalogNum", CatalogNum);
		Integer i=manage.insert(
				"com.ymttest.database.mapping.ExpMapper.insertOrderCatalogNum", m);
		manage.commit();
		return i;
	}
	
	/**
	 * 删除记录
	 * @param OrderId
	 * @return
	 */
	public Integer deleteOrderCatalogNum(Long OrderId){
		Map m=new HashMap<>();
		m.put("OrderId", OrderId);
		Integer i=manage.insert(
				"com.ymttest.database.mapping.ExpMapper.deleteOrderCatalogNum", m);
		manage.commit();
		return i;
	}
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	
	public static void main(String[] args) throws InterruptedException {
//		FestivalRateConfig frc=new FestivalRateConfig();
//		frc.setRate(1d);
//		frc.setDate(YMTDateUtil.getSysTime("yyyy-MM-dd"));
//		frc.setCreateTime(YMTDateUtil.getDate());
//		frc.setEnable(true);
//		frc.setRemark("autotest");
//		frc.setLastUpdateTime(YMTDateUtil.getDate());
//		System.out.println(new ExpWapper().insertFestivalRateConfig(frc));
//		System.out.println(new ExpWapper().deleteFestivalRateConfig((frc.getDate())));
		ExpWapper ExpWapper=new ExpWapper();
		for(int i=0;i<1000000;i++){
		int coin=15;
		UserCoin uc=new UserCoin();
		uc.setCoin(coin);
		uc.setUserId(i);
		uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-1);
		ExpWapper.insertUserCoin(uc);
		}
//		System.out.println(FormatUtil.beanToGSONString(new ExpWapper().selectUserCoin(1, "2018")));
//		System.out.println(FormatUtil.beanToGSONString(new ExpWapper().selectUserCoinRecord(1, null,null,null)));
//		System.out.println(FormatUtil.beanToGSONString(new ExpWapper().selectuserexp(4085)));
//		System.out.println(FormatUtil.beanToGSONString(new ExpWapper().selectuserexpdetail(4085,null,null,null)));
	}

	
}
